package kz.kstu.ds.sign;

import kz.kstu.ds.chooser.filter.KeyFileFilter;
import kz.kstu.ds.chooser.filter.TextFileFilter;
import kz.kstu.ds.chooser.filter.XMLFileFilter;
import kz.kstu.ds.signature.SignatureGenerator;
import kz.kstu.ds.utils.GUIFactory;
import kz.kstu.ds.utils.GUIIcon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Date: 04.11.12
 * Time: 22:52
 *
 * @author Oleg Anastassov
 */
public class SignFrame extends JFrame {

    private static final String TITLE = "Подписывание документа";

    private static final int HEIGHT = 200;
    private static final int WIDTH = 500;

    private JTextField fileTextField;
    private JButton fileButton;
    private JButton keyButton;
    private JTextField keyTextField;
    private JButton signButton;
    private JPanel rootPanel;

    public SignFrame() {
        setTitle(TITLE);
        setSize(new Dimension(WIDTH, HEIGHT));
        setResizable(false);
        setContentPane(rootPanel);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        initIcons();
        initListeners();
    }

    private void initIcons() {
        setIconImage(GUIFactory.getImage(GUIIcon.LOGO));
        fileButton.setIcon(GUIFactory.getIcon(GUIIcon.CARD));
        keyButton.setIcon(GUIFactory.getIcon(GUIIcon.KEY));
        signButton.setIcon(GUIFactory.getIcon(GUIIcon.SIGN));
    }

    private void initListeners() {
        fileButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser(new File("."));
                fc.setFileFilter(new TextFileFilter());

                int result = fc.showOpenDialog(rootPanel);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File f = fc.getSelectedFile();
                    fileTextField.setText(f.getAbsolutePath());
                }
            }
        });
        keyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser(new File("."));
                fc.setFileFilter(new KeyFileFilter());

                int result = fc.showOpenDialog(rootPanel);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File f = fc.getSelectedFile();
                    keyTextField.setText(f.getAbsolutePath());
                }
            }
        });
        signButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser(new File("."));
                fc.setFileFilter(new XMLFileFilter());

                int result = fc.showSaveDialog(rootPanel);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File out = fc.getSelectedFile();
                    SignatureGenerator sg = new SignatureGenerator();
                    sg.sign(
                            new File(fileTextField.getText()),
                            out,
                            new File(keyTextField.getText())
                    );
                }
            }
        });
    }
}
